package la.bbs558.controller.admin;

import cn.dev33.satoken.annotation.SaCheckRole;
import cn.dev33.satoken.annotation.SaIgnore;
import la.bbs558.aop.valid.Check;
import la.bbs558.controller.base.Result;
import la.bbs558.entity.BbsPost;
import la.bbs558.entity.enums.PostProperty;
import la.bbs558.entity.enums.PostState;
import la.bbs558.entity.params.SendReplyForAdminParams;
import la.bbs558.entity.params.SendPostForAdminParams;
import la.bbs558.entity.params.PostSearchPage;
import la.bbs558.satoken.StpAdminUtil;
import la.bbs558.service.BbsPostService;
import la.bbs558.util.EnumUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

/**
 * @author: Shanks
 * @date: 2023/12/16 12:27
 * @desc:
 */
@RestController
@RequestMapping("/admin/post")
@RequiredArgsConstructor
@SaCheckRole(value = { "admin" },type = StpAdminUtil.TYPE)
@Check
public class BbsPostManageController {

    private final BbsPostService bbsPostService;

    @GetMapping("propertyList")
    public Result<?> getPostPropertyList() {
        return Result.success(EnumUtils.toList(PostProperty.class, "desc", "code"));
    }

    @GetMapping("stateList")
    public Result<?> getPostStateList() {
        return Result.success(PostState.getStateList());
    }

    @GetMapping("list")
    public Result<?> list(PostSearchPage searchPage) {
        bbsPostService.listByPageForAdmin(searchPage);
        return Result.success(searchPage);
    }

    @GetMapping("get")
    public Result<?> getPostById(Long id) {
        return Result.success(bbsPostService.getPostByIdForAdmin(id));
    }

    @PostMapping("audit")
    public Result<?> audit(@RequestBody BbsPost bbsPost) {
        bbsPostService.auditForAdmin(bbsPost);
        return Result.success();
    }

    @PostMapping("highLight")
    public Result<?> highLightPost(Long id, Integer highLight) {
        bbsPostService.highLightPost(id, highLight);
        return Result.success();
    }

    @PostMapping("top")
    public Result<?> topPost(Long id, Integer top) {
        bbsPostService.lambdaUpdate()
                .eq(BbsPost::getId, id)
                .set(BbsPost::getTop, top == 1 ? 1 : 0)
                .set(BbsPost::getTopTime, new Date())
                .update();
        return Result.success();
    }

    @PostMapping("delete")
    public Result<?> delete(Long id) {
        bbsPostService.removePostByIdForAdmin(id);
        return Result.success();
    }

    @PostMapping("send")
    @SaIgnore
    public Result<?> send(SendPostForAdminParams params) {
        return Result.success(bbsPostService.sendForAdmin(params));
    }

    /**
     * 手动回复帖子
     * @param params
     * @return
     */
    @PostMapping("sendReply")
    @SaIgnore
    public Result<?> sendReply(SendReplyForAdminParams params) {
        return Result.success(bbsPostService.sendReplyForAdmin(params));
    }

}
